package bm.com.project.server.project.service.impl;

import bm.com.framework.security.util.SecurityUtils;
import bm.com.project.server.common.dto.SearchBaseDTO;
import bm.com.project.server.project.dto.BmProjectMilestoneDTO;
import bm.com.project.server.project.entity.BmProjectMilestoneEntity;
import bm.com.project.server.project.mapper.BmProjectMilestoneMapper;
import bm.com.project.server.project.service.BmProjectMilestoneService;
import bm.com.framework.common.resp.PageVO;
import bm.com.framework.common.resp.ResultVO;
import bm.com.framework.common.unit.DateUnit;
import bm.com.framework.common.unit.Tool;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;


@Service("bmProjectMilestoneService")
public class BmProjectMilestoneServiceImpl extends ServiceImpl<BmProjectMilestoneMapper, BmProjectMilestoneEntity> implements BmProjectMilestoneService {

    @Resource
    BmProjectMilestoneMapper bmProjectMilestoneDao;

    /**
     * 创建里程碑信息
     * @param bmProjectMilestoneDTO
     * @return
     */
    @Override
    public ResultVO create(BmProjectMilestoneDTO bmProjectMilestoneDTO) {
        BmProjectMilestoneEntity bmProjectMilestoneEntity = new BmProjectMilestoneEntity();

        bmProjectMilestoneEntity.setId(Tool.iDGenerator());
        bmProjectMilestoneEntity.setProjectId(bmProjectMilestoneDTO.getProjectId());
        bmProjectMilestoneEntity.setOrgId(SecurityUtils.getOrgId());
        bmProjectMilestoneEntity.setCreater(SecurityUtils.getLoginUserId());
        bmProjectMilestoneEntity.setCreateDate(DateUnit.getNowTimeSecond());

        if (!Tool.isLongEmpty(bmProjectMilestoneDTO.getStartTime())){
            bmProjectMilestoneEntity.setStartTime(bmProjectMilestoneDTO.getStartTime());
        }

        if (!Tool.isLongEmpty(bmProjectMilestoneDTO.getEndTime())){
            bmProjectMilestoneEntity.setEndTime(bmProjectMilestoneDTO.getEndTime());
        }

        bmProjectMilestoneEntity.setStatus(bmProjectMilestoneDTO.getStatus());
        bmProjectMilestoneEntity.setTitle(bmProjectMilestoneDTO.getTitle());
        bmProjectMilestoneEntity.setMark(bmProjectMilestoneDTO.getMark());
        bmProjectMilestoneDao.insertData(bmProjectMilestoneEntity);

        return ResultVO.success("操作成功");
    }

    /**
     * 获取列表消息
     * @param queue
     * @return
     */
    @Override
    public ResultVO getList(SearchBaseDTO queue) {

        queue.setOrgId(SecurityUtils.getOrgId());
        int size = bmProjectMilestoneDao.selectPagelist(queue).size();

        queue.setPageOffset((queue.getPage() - 1) * queue.getPageNum());
        List<BmProjectMilestoneEntity> list = bmProjectMilestoneDao.selectPagelist(queue);

        PageVO<Object> PageVO = new PageVO<>(size, list);
        return ResultVO.success(PageVO);
    }
}